package com.dragon.design.pattern.structural.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/7/1 19:19
 */
public class DefaultProxy implements MethodInterceptor {

    public Object getInstance(Object object) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(object.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("我是媒婆，得给你找个异性");
        System.out.println("开始筛选");
        methodProxy.invokeSuper(obj, args);
        System.out.println("如果合适的话，就准备办事");
        return null;
    }
}
